package com.hf.eagle.web.struts;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.DynaBean;
import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.hf.eagle.domain.Accessory;
import com.hf.eagle.domain.AdvertisementConfig;
import com.hf.eagle.domain.NewsInfo;
import com.hf.eagle.domain.ProductInfo;
import com.hf.eagle.domain.SiteLink;
import com.hf.eagle.web.base.BaseAction;
import com.hf.eagle.web.base.Pager;
import com.hf.eagle.web.base.SysConstant;

/**
 * 广告宣传
 * @author xiaokeheng
 *
 */
public class AdvertisingAction extends BaseAction {

	public ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		return this.view(mapping, form, request, response);
	}

	/**
	 * 显示广告宣传
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward view(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		DynaBean lazyForm = (DynaBean) form;
		String mod_id = (String) lazyForm.get("mod_id");
        request.setAttribute("mod_id", mod_id);
		return mapping.findForward("view");
	}
	
	/**
	 * 显示广告宣传列表
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward list(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		DynaBean lazyForm = (DynaBean) form;
		Pager pager2810 = (Pager) lazyForm.get("pager");
		String mod_id = (String) lazyForm.get("mod_id");
        request.setAttribute("mod_id", mod_id);
        SiteLink advertisingSiteLink=new SiteLink();
		advertisingSiteLink.setIs_del(0);
		advertisingSiteLink.setMod_id((long) 2810);
		List<SiteLink> advertisingSiteLinks = getFacade().getSiteLinkService().getSiteLinkList(advertisingSiteLink);
		for (SiteLink siteLink : advertisingSiteLinks) {
			if (null != siteLink) {
				Accessory acce = new Accessory();
				acce.setFile_level(SysConstant.SUB_IMG);
				acce.getMap().put("mod_id", "2810");
				acce.getMap().put("record_id", siteLink.getId());
				acce = super.getFacade().getAccessoryService().getAccessory(acce);
				siteLink.setAccessory(acce);
			}
		}
		request.setAttribute("advertisingSiteLinks", advertisingSiteLinks);
		
		return mapping.findForward("list");
	}
	
	/**
	 * 显示广告列表
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward adv(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		DynaBean lazyForm = (DynaBean) form;
		String mod_id = (String) lazyForm.get("mod_id");
		AdvertisementConfig ac=new AdvertisementConfig();
		ac.setMod_id(Long.valueOf(mod_id));
		ac=super.getFacade().getAdvertisementConfigService().getAdvertisementConfig(ac);
		StringBuffer sf = new StringBuffer("[");
		boolean is_first=false;
		long ad_mod_id=3200;
		if(ac!=null) ad_mod_id=ac.getAd_mod_id();
		if(2902==ad_mod_id){
			NewsInfo newsInfo = new NewsInfo();
			newsInfo.setIs_del(0);
			newsInfo.setMod_id((long) 2902);

			Long recordCount = getFacade().getNewsInfoService().getNewsInfoCount(newsInfo);
			Pager pager = new Pager();
			pager.init(recordCount, 10, "1");
			newsInfo.getRow().setFirst(pager.getFirstRow());
			newsInfo.getRow().setCount(pager.getLastRow());
			List<NewsInfo> list2902 = super.getFacade().getNewsInfoService().getNewsInfoPaginatedList(newsInfo);
			if (null != list2902 && list2902.size() > 0) {
				for (NewsInfo newsInfo2 : list2902) {
					if (null != newsInfo2) {
						Accessory acce = new Accessory();
						acce.setFile_level(SysConstant.SUB_IMG);
						acce.getMap().put("mod_id", "2902");
						acce.getMap().put("record_id", newsInfo2.getId());
						acce = super.getFacade().getAccessoryService().getAccessory(acce);
						newsInfo2.setAccessory(acce);
					}
				}
			}
			for (NewsInfo newsInfo2 : list2902) {
				String title=newsInfo2.getTitle();
				String img_url="";
				if(newsInfo2.getAccessory()!=null){
					img_url=newsInfo2.getAccessory().getSave_url();
				}
				String target_url="SalesPromotionDynamics.do?method=view&mod_id=2902&id="+newsInfo2.getId();
				if(is_first){
					sf.append(",");
				}
				sf.append("{'title':'"+title+"','model':'','img_url':'"+img_url+"','target_url':'"+target_url+"'}");
				is_first=true;
			}
			
		}else{
			ProductInfo productInfo = new ProductInfo();
			productInfo.setIs_del(0);
			productInfo.setIs_recommend(1);
			List<ProductInfo> list = super.getFacade().getProductInfoService().getProductInfoList(productInfo);
			for (ProductInfo productInfo2 : list) {
				Accessory acce = new Accessory();
				acce.setIs_del(0);
				acce.setFile_level(SysConstant.SUB_IMG);
				acce.getMap().put("mod_id", "3200");
				acce.getMap().put("record_id", productInfo2.getProduct_id());
				acce = super.getFacade().getAccessoryService().getAccessory(acce);
				productInfo2.setAccessory(acce);
			}
			for (ProductInfo productInfo2 : list) {
				String img_url="";
				if(productInfo2.getAccessory()!=null){
					img_url=productInfo2.getAccessory().getSave_url();
				}
				String target_url="ProductDisplay.do?method=list&mod_id=3200&is_recommend=1";
				if(is_first){
					sf.append(",");
				}
				sf.append("{'title':'"+productInfo2.getProduct_name()+"','model':'"+productInfo2.getProduct_model()+"','img_url':'"+img_url+"','target_url':'"+target_url+"'}");
				is_first=true;
			}
		}
		sf.append("]");
		super.renderJson(response, sf.toString());
		//request.setAttribute("salesPromotionDynamics", salesPromotionDynamics);
		return null;
	}

}